﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="GetSchedulerJobCmdlet.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace CloudBlitz.Powershell.Cmdlets
{
    using System;
    using System.Collections.Generic;
    using System.Management.Automation;

    [Cmdlet(VerbsCommon.Get, "SchedulerJob")]
    public class GetSchedulerJobCmdlet : HpcSchedulerCmdletBase
    {
        [Parameter(Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public int JobId { get; set; }

        [Parameter]
        public string Properties { get; set; }

        protected override void ProcessRecord()
        {
            try
            {
                base.ProcessRecord();
                this.GetSchedulerJobProcess();
            }
            catch (Exception ex)
            {
                this.ThrowTerminatingError(this.HandleException(ex).Result);
            }
        }

        private void GetSchedulerJobProcess()
        {
            var client = this.CreateClient();

            IEnumerable<string> propertiesToList = null;
            if (!string.IsNullOrEmpty(this.Properties))
            {
                propertiesToList = this.Properties.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            }

            var job = client.GetJob(this.Credentials.ClusterName, this.JobId, propertiesToList).Result;

            this.WriteObject(job);
        }
    }
}